package com.mewpipe.servlet;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mewpipe.dao.CommentDao;
import com.mewpipe.dao.DaoFactory;
import com.mewpipe.dao.LnkUserLikeDao;
import com.mewpipe.entity.Comment;
import com.mewpipe.entity.UserSession;
import com.mewpipe.entity.Video;

/**
 * Servlet implementation class VoteCommentServlet
 */
@WebServlet("/commentvote")
public class VoteCommentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private LnkUserLikeDao votedao;
	private CommentDao commentDao;

	public void init(ServletConfig config) throws ServletException {
		votedao = DaoFactory.getInstance().getLnkUserLikeDao();
		commentDao = DaoFactory.getInstance().getCommentDao();
	}

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		boolean typeLike;
		long idComment;
		Comment commentvote=null;
				boolean testvote= false;
		  
		HttpSession session = request.getSession();
		UserSession usersession = (UserSession) session.getAttribute("UserSession");
		
		
		idComment = Long.parseLong(request.getParameter("id"));
		long userid = usersession.getId();
		testvote = votedao.CheckVoteCom(userid, idComment);
		commentvote = commentDao.GetCommentById(idComment); 
			if (!testvote)
			{
			if (request.getParameter("type").equals("LIKE")) {
				typeLike=false;
				commentvote.incrementlike();
				
				
				}
				else{
					typeLike=true;
					commentvote.incrementdislike();
					
					}
			 commentDao.updateComment(commentvote);
			 votedao.addLink(usersession.getId(),idComment , typeLike,true);
		}	
	}	

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

}
